<?php
namespace app\common\bonus;

Class Fuwu extends Common
{
	static $bonus_name='fuwu';

	public function handle($data){
		$actionData = $data['action_data'];

		$reason_time = $data['create_time'];
		$reason_day = date('Ymd',$reason_time);

		$uid = $actionData['uid'];
		$score = $actionData['score'];
		$agent_uid = $actionData['agent_uid'];

		// 获取本奖金配置数据
		$bonusConfigModel = model('bonus_config');
		// 货币配置
		$coinConfig = $bonusConfigModel::bonusConfig(self::$bonus_name,'coin');
		$coinConfig = unserialize($coinConfig['config_value']);
		// 代理中心配置
		$agentConfig = $bonusConfigModel::bonusConfig(self::$bonus_name,'agent');
		$agentConfig = unserialize($agentConfig['config_value']);
		// 获取该会员所在代理中心的会员等级
		$memberModel = new \app\common\model\Member;
		$agent_level_id = $memberModel->where(['id'=>$agent_uid])->value('level_id');
		// 根据代理等级获取配置0
		$agentConfig = array_combine(array_column($agentConfig,'level_id'),$agentConfig);
		$config = $agentConfig[$agent_level_id];
		$rate = $config['rate'];

		// 计算奖金
		$bonus = $score*$rate/100;

		$memberModel->startTrans();
		try{
			// 累计奖金
			$this->incTotal($bonus,$agent_uid,$reason_day,self::$bonus_name);
			// 货币分配
			$this->bonus_record($coinConfig,self::$bonus_name,$uid,$agent_uid,$bonus,$reason_time);

			$memberModel->commit();
		}catch(\Exception $e){
			$memberModel->rollback();
			$this->error = $e->getMessage();
			return false;
		}
		return true;
		
	}

}